using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
using AzureMVC4;
using AzureMVC4.Models;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Telerik.OpenAccess;


namespace UpdaterRole
{
    public class WorkerRole : RoleEntryPoint
    {
        static AzureMVC4Context dbContext = new AzureMVC4Context();
        static ISchemaHandler schemaHandler = dbContext.GetSchemaHandler();

        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("UpdaterRole entry point called", "Information");



            while (true)
            {
                Thread.Sleep(5000);
                Trace.WriteLine("Working", "Information");
                //TEST SHIT

                Product p = new Product();
                p.ID = 1;
                p.ProductName = "MyProduct";
                p.Price = 1;
               // dbContext.Add(p);
               // dbContext.SaveChanges();
                

                foreach (Product item in dbContext.Products)
                {
                  //  Trace.WriteLine(item.ProductName);

                }

                readXML();
            }
        }

        private void readXML()
        {

            XDocument loaded = XDocument.Load("http://andrewscarpetcleaning.com/test.xml");
            // Query the data and write out to a list of objects
            List<SMJobXMLHack> jlist = (from c in loaded.Descendants("smJob")
                         select new SMJobXMLHack
                          {
                              Address = (string)c.Element("address"),
                              Date = (string)c.Element("date"),
                              Name = (string)c.Element("name"),
                              Techs = (string)c.Element("techs"),

                          }).ToList<SMJobXMLHack>();

            persistJobs(jlist);

        }

        private void persistJobs(List<SMJobXMLHack> joblist)
        {
           
        }



        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            if (schemaHandler.DatabaseExists() == false)
            {
                schemaHandler.CreateDatabase();
                schemaHandler.ExecuteDDLScript(schemaHandler.CreateDDLScript());
            }
            return base.OnStart();
        }
    }

 
}

